import { Module } from 'vuex'
import { RootState } from '../index'
import { IntegralState, IntegralItem } from '../../types/integral'
import { PageParams } from '../../types/common'
import { api } from '../../api'

const integral: Module<IntegralState, RootState> = {
  namespaced: true,
  
  state: {
    balance: 0,
    list: []
  },

  mutations: {
    SET_BALANCE(state: IntegralState, balance: number) {
      state.balance = balance
    },
    SET_LIST(state: IntegralState, list: IntegralItem[]) {
      state.list = list
    }
  },

  actions: {
    async fetchIntegralList({ commit }, params: PageParams) {
      const res = await api.getIntegralList(params)
      if (res.code === 0) {
        commit('SET_LIST', res.data.list)
      }
      return res
    }
  }
}

export default integral 